package com.jiuyuan.service.impl;

import com.jiuyuan.entity.Department;
import com.jiuyuan.entity.vo.RespBean;
import com.jiuyuan.mapper.DepartmentMapper;
import com.jiuyuan.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiuyuan
 * @since 2023-02-15
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {
    @Autowired
    DepartmentMapper departmentMapper;

    @Override
    //查询所有部门
    public List<Department> allList() {
        return departmentMapper.allList(-1);
    }

    @Override
    public RespBean add(Department department) {
        department.setEnabled(true);
        departmentMapper.add(department);
        if(department.getResult()==1) return RespBean.success("添加成功",department);
        return RespBean.error("添加失败");
    }

    @Override
    public RespBean delete(Integer id) {
        //存储过程进行删除
//        Department department = departmentMapper.selectById(id);
        //新建一个department，后面需要获取里面的result
        Department department = new Department();
        department.setId(id);
        departmentMapper.deleteWithCallback(department);
        if(-2==department.getResult()) return RespBean.error("此部门为父部门");
        if(-1==department.getResult()) return RespBean.error("此部门下还有员工");
        if(1==department.getResult()) return RespBean.success("删除成功");
        return RespBean.error("删除失败");
    }
}
